package com.hotel.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hotel.entity.User;
import com.hotel.entity.UserResult;
import com.hotel.service.LoginService;
import com.hotel.service.RegisterService;
import com.hotel.service.UpdateService;
import com.hotel.utils.LoginVerification;
import com.hotel.utils.Message;
import com.hotel.utils.RegisterVerification;





//处理用户账户模块的注册、登录和修改

@Controller
@RequestMapping("/user")
public class UserAccountController {
	
	@Resource  
    private RegisterService registerService;  
	@Resource  
    private LoginService loginService;  
	@Resource  
    private UpdateService updateService;  
	
	@ResponseBody
    @RequestMapping(value="/register", method=RequestMethod.POST)  
    public UserResult userRegisterToPost(@RequestBody User user,HttpServletRequest request,HttpServletResponse response){
		UserResult ur = new UserResult();
    	String message;
    	
    	//验证用户信息是否完整
    	message = RegisterVerification.Verification(user);
    	
    	if(Message.Message_0002 == message){
    		//验证用户名是否存在
        	message = registerService.nameIsNotExist(user);
    	}
    	else if(Message.Message_1002 == message){
    		ur.setMessage(message);
    		ur.setUser(null);
    	}
    	
    	
    	//验证成功，用户名不存在
    	if(Message.Message_0000 == message){
    		message = registerService.putUser(user);
    	}
    	//验证失败，用户名存在
    	else if(Message.Message_1000 == message){
    		ur.setMessage(message);
    		ur.setUser(null);
    	}
        
    	//注册成功
    	if(Message.Message_0001 == message){
    		ur.setMessage(message);
    		ur.setUser(user);

    	}
    	//注册失败
    	else if(Message.Message_1001 == message){
    		ur.setMessage(message);
    		ur.setUser(null);
    	}
    	
    	return ur;
    }  
    

    @ResponseBody
    @RequestMapping(value="/login", method=RequestMethod.POST)  
    public UserResult userLoginToPost(@RequestBody User user,HttpServletRequest request,HttpServletResponse response){
    	UserResult ur = new UserResult();
    	String message;
    	
    	//完整性验证
    	message = LoginVerification.Verification(user);
    	if(Message.Message_2001 == message){
    		//正确性验证
        	message = loginService.Verification(user);
    	}
    	else{
    		ur.setMessage(message);
    		ur.setUser(null);
    	}

    	//匹配成功
    	if(Message.Message_2000 == message){
    		ur.setMessage(message);
    		ur.setUser(user);
    		request.getSession().setAttribute("account", user.getAccount());
    	}
    	//验证失败，用户名不存在
    	else if(Message.Message_3000 == message){
    		ur.setMessage(message);
    		ur.setUser(null);
    	}
    	//验证失败，用户名或密码输入错误
    	else if(Message.Message_3001 == message){
    		ur.setMessage(message);
    		ur.setUser(null);
    	}
    	
    	return ur;
    }  
    
    
    @ResponseBody
    @RequestMapping(value="/update", method=RequestMethod.POST)  
    public UserResult userUpdateToPost(@RequestBody User user,HttpServletRequest request,HttpServletResponse response){
    	UserResult ur = new UserResult();
    	UserResult ur2 = new UserResult();
    	UserResult updateUr = new UserResult();
    	String message = Message.Message_6000;
    	
    	
    	String username;
    	username = (String) request.getSession().getAttribute("account");
    	
    	System.out.println(username);
    	
    	if(null != username){
    		ur2 = updateService.selectName(username);
    		message = ur2.getMessage();
    	}
    	else if(null == username || "" == username){
    		ur.setMessage(message);
    		ur.setUser(null);
    	}
    	System.out.println(ur2.getUser().getAccount());
    	//找到用户
    	if(Message.Message_4000 == message){
    		if(username.equals(user.getAccount())){
    			user.setId(ur2.getUser().getId());
    			updateUr = updateService.updateUser(user);
        		message = updateUr.getMessage();
    		}
    		else{
    			ur.setMessage(Message.Message_5002);
    			ur.setUser(null);
    		}
    	}
    	//未找到用户
    	else if(Message.Message_5000 == message){
    		ur.setMessage(message);
    		ur.setUser(null);
    	}
    	
    	//更新成功
    	if(Message.Message_4001 == message){
    		ur.setMessage(message);
    		ur.setUser(updateUr.getUser());
    	}
    	else if(Message.Message_5001 == message){
    		ur.setMessage(message);
    		ur.setUser(null);
    	}
    	return ur;
    } 
}
